/**
 * Created by vincent on 2016/12/7.
 */
function mergeData(data) {
    var constantparam = "{languageColumn:'name_eu'},{'Content-Type':'application/x-www-form-urlencoded'}";
    if (data == undefined || data == null) {
        return data;
    } else {
        return $.extend({}, data, constantparam);
    }
}

function extend(o, n, override) {
    for (var p in n) if (n.hasOwnProperty(p) && (!o.hasOwnProperty(p) || override)) o[p] = n[p];
}

function guidGenerator() {
    var S4 = function () {
        return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
    };
    return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}

function isJson(obj) {
    var isjson = typeof(obj) == "object" && Object.prototype.toString.call(obj).toLowerCase() == "[base base]" && !obj.length;
    return isjson;
}

function getQueryString(url, name) {
    var paras = url.split("?");
    if (paras.length == 2) {
        paras = paras[1];
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = paras.match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    } else {
        return null;
    }

}


function base64decode(str) {
    var c1, c2, c3, c4;
    var i, len, out;
    len = str.length;
    i = 0;
    out = "";
    while (i < len) {
        /* c1 */
        do {
            c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
        }
        while (i < len && c1 == -1);
        if (c1 == -1)
            break;
        /* c2 */
        do {
            c2 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
        }
        while (i < len && c2 == -1);
        if (c2 == -1)
            break;
        out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));
        /* c3 */
        do {
            c3 = str.charCodeAt(i++) & 0xff;
            if (c3 == 61)
                return out;
            c3 = base64DecodeChars[c3];
        }
        while (i < len && c3 == -1);
        if (c3 == -1)
            break;
        out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2));
        /* c4 */
        do {
            c4 = str.charCodeAt(i++) & 0xff;
            if (c4 == 61)
                return out;
            c4 = base64DecodeChars[c4];
        }
        while (i < len && c4 == -1);
        if (c4 == -1)
            break;
        out += String.fromCharCode(((c3 & 0x03) << 6) | c4);
    }
    return out;
}

function singleton(fn) {
    var result;
    return function () {
        return result || (result = fn.apply(this, arguments));
    };
}

/**
 * @return {string}
 */
function replaceEnter(mystr) {
    if (mystr == null || mystr == "") {
        return "";
    }
    else {
        return mystr.replace(/\n/g, "<br/>");
    }
}

function delHtmlTag(str) {
    return str.replace(/<[^>]+>/g, "");//去掉所有的html标记
}

function getParamsObjFromUrl(url) {
    var obj = {};
    if (StringUtils.isNlOrUndOrEmpty(url)) {
        return obj;
    } else {
        var wh = url.lastIndexOf('?');
        if (wh != -1) {
            var search = url.substr(wh);
            obj = getUrlRequest(search);
        }

    }
    return obj;
}

function getUrlFromParamsObj(url, paramsObj) {
    var wh = url.lastIndexOf('?');
    var u = url;
    if (wh == -1) {
        u = u + "?";
    } else {
        u = url.substring(0, wh + 1);
    }
    for (var key in paramsObj) {
        if (paramsObj.hasOwnProperty(key)) {
            var value = paramsObj[key];
            u = u + key + "=" + value + "&";
        }
    }
    u = u.substr(0, u.length - 1);
    return u;
}

function getUrlRequest(url) {
    var theRequest = {};
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        if (str.indexOf("&") != -1) {
            strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
            }
        } else {
            theRequest[str.split("=")[0]] = unescape(str.split("=")[1]);
        }
    }
    return theRequest;
}

function getColors(size) {
    var colors = new Array();
    for (var i = 0; i < size; i++) {
        var color = '#' + Math.floor(Math.random() * 0xffffff).toString(16);
        colors.push(color);
    }
    return colors;
}